﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
using UnityEngine;



public class MyInfo1
{
    public int num1;
    private int num2;
    protected int num3;
    internal int num4;

    public string name;

    public NewItem item;

    public List<int> intList;

    public List<NewItem> itemList;

    //不支持字典
    //public Dictionary<int, NewItem> dic = new Dictionary<int, NewItem>() { { 12,new NewItem(54545,45,"dd") } };

}

public class NewItem
{
    //通过特性设置属性名称
    public int id;
    public float num;
    public string describe;
}


/// <summary>
/// 序列化成xml文件
/// </summary>
public class XmlSerialize : MonoBehaviour
{
    private void Start()
    {
        //首先得有可存储的文件
        string path = Application.persistentDataPath + "/serializeTest.xml";

        //等待被写入的对象
        MyInfo1 myInfo1 = new MyInfo1();

        //打开文件写入流
        //using语句，会管理括号中的对象的生命周期，当语句结束时，会自动释放括号中的对象，其实就是调用对象中的Dispose()函数
        //streamWrite类，如果路径地址上的文件存在，就打开，否则创建一个
        using (StreamWriter sw = new StreamWriter(path))
        { 
            //设置要序列化的对象类型，即准备膜具
            XmlSerializer xs = new XmlSerializer(typeof(MyInfo1));

            xs.Serialize(sw, myInfo1);
        }
    }
}
